package com.redwine.app;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.redwine.app.adapter.MyOfferAdapter;
import com.redwine.app.net.GsonRequest;
import com.redwine.app.net.NetworkConstants;
import com.redwine.app.net.responses.MyOfferResponse;
import com.redwine.app.utils.Global;
import com.redwine.app.utils.NetUtil;
import com.redwine.app.view.EmptyView;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static com.redwine.app.utils.Utils.showToast;

/**
 * 我的报价
 * 作者：gassion on 17/9/9 18:34
 * 最可怕的敌人，就是没有坚强的信念。
 */
public class MyOfferActivity extends BaseActivity {

    @ViewInject(R.id.header_title)
    private TextView headerTitle;
    @ViewInject(R.id.layout_back)
    private LinearLayout layoutBack;
    private Intent intent;
    private int page = 1;//默认加载第一页
    @ViewInject(R.id.my_offer_refresh_list_view)
    private PullToRefreshListView mListView;
    @ViewInject(R.id.empty_view)
    private EmptyView emptyView;
    private MyOfferAdapter myOfferAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_offer);
        ViewUtils.inject(this);
        initView();
        requestMyPriceListData(true);
    }

    private void initView() {
        headerTitle.setText(R.string.my_offer);
        layoutBack.setVisibility(View.VISIBLE);
        myOfferAdapter = new MyOfferAdapter(this);
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                requestMyPriceListData(false);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page += 1;
                requestMyPriceListData(false);
            }
        });
        mListView.setOnActionMoveListener(new PullToRefreshBase.OnActionMove() {
            @Override
            public void onMove(boolean isMove) {
                setUpdateTime(mListView);
            }
        });
        mListView.setAdapter(myOfferAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MyOfferResponse.DataEntityX.DataEntity dataEntity = (MyOfferResponse.DataEntityX.DataEntity) parent.getItemAtPosition(position);
                MyOfferResponse.DataEntityX.DataEntity.PublishInfoEntity publish_info = dataEntity.getPublish_info();
                Intent intent = new Intent(MyOfferActivity.this, ProcurementDetailActivity.class);
                intent.putExtra(PublishedSourcesDetailActivity.SHOP_NAME, publish_info.getNickname());
                intent.putExtra(PublishedSourcesDetailActivity.SHOP_UID, publish_info.getUid());
                intent.putExtra(PublishedSourcesDetailActivity.SHOP_ID, publish_info.getId());
                startActivity(intent);
            }
        });
        mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        mListView.setEmptyView(emptyView);
        emptyView.bind();
    }

    @OnClick({R.id.layout_back})
    public void mClick(View v) {
        switch (v.getId()) {

            case R.id.layout_back:
                finish();
                break;
        }
    }

    /**
     * 收获地址列表
     */
    private void requestMyPriceListData(final boolean isLoading) {
        if (!NetUtil.checkNet(this)) {
            showToast(R.string.generic_check);
            setErrorView(emptyView, mListView);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (null != mListView)
                        mListView.onRefreshComplete();

                }
            }, 500);
            return;
        }

        if (isLoading)
            showWaiting();
        String url = NetworkConstants.MY_PRICE_URL;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("uid", Global.user.getUid());
        GsonRequest<MyOfferResponse> request = new GsonRequest<>(url, Request.Method.POST,
                MyOfferResponse.class, false, params, new Response.Listener<MyOfferResponse>() {
            @Override
            public void onResponse(MyOfferResponse response) {
                if (isLoading)
                    dismissWaiting();
                if (null != mListView)
                    mListView.onRefreshComplete();
                if (response.isSuccess()) {
                    MyOfferResponse.DataEntityX responseData = response.getData();
                    List<MyOfferResponse.DataEntityX.DataEntity> responseDataData = responseData.getData();

                    if (null != responseDataData && !responseDataData.isEmpty()) {
                        myOfferAdapter.setData(page, responseDataData);
                    } else {
                        setEmptyView(emptyView, mListView);
                    }
                    if (responseData.getPage() != responseData.getAllpage()) {
                        mListView.setMode(PullToRefreshBase.Mode.BOTH);
                    } else {
                        mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                    }
                } else {
                    setEmptyView(emptyView, mListView);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (isLoading)
                    dismissWaiting();
                showToast(R.string.generic_check);
                setEmptyView(emptyView, mListView);
            }
        });
        APP.getInstance().getRequestQueue().add(request);
    }

    @Override
    protected void emptyOnclick() {
        emptyView.bind();
        if (null != mListView) {
            mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    mListView.setRefreshing();
                }
            }, 500);
        } else {
            requestMyPriceListData(true);
        }
    }

}
